

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.Socket;
import java.net.URL;
import java.net.UnknownHostException;
import java.util.List;

import pjaws.PjawsConstants;

import com.amazon.aes.webservices.client.Jec2;
import com.amazon.aes.webservices.client.RequestResultPair;
import com.amazon.aes.webservices.client.ReservationDescription;
import com.amazon.aes.webservices.client.ReservationDescription.Instance;

public class EC2Sample {
	public static void main(String args[]) {

		String currDir = System.getProperty("user.dir");
		String urlStr = "http://ec2.amazonaws.com/";
		URL url = null;
		try {
			url = new URL(urlStr);
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String key = currDir + "/pk-PEXNMTQQYNTJAPRC5H62B3KO7NO55QOV.pem"; 
		String cert = currDir + "/cert-PEXNMTQQYNTJAPRC5H62B3KO7NO55QOV.pem"; 

		System.out.println(urlStr);
		System.out.println(key);
		System.out.println(cert);

		try {
			//create ec2 handler
			Jec2 myJec2 = new Jec2(url, key, cert);
			
			// spawn ec2 instance
			RequestResultPair<ReservationDescription> runResults = myJec2
					.runInstances("ami-4ea34727", 1, 1, null, null,
							PjawsConstants.KEYPAIR, null, null, null, null,
							null, null, null);

			
			// get instance id
			System.out.println(runResults.getResponse());
			Instance myInstance = runResults.getResponse().instances.get(0);
			String instanceID = myInstance.instanceId;
			String[] instanceIDs = { instanceID };
			
			//String instanceID = "i-4ef74927";
			//String[] instanceIDs = { instanceID };

			// try getting hostname immediately after spawning
			// get hostname
			RequestResultPair<List<ReservationDescription>> describeResults = myJec2
					.describeInstances(instanceIDs);
			String hostname = describeResults.getResponse().get(0).instances
					.get(0).dnsName;
			System.out.println("hostname before: " + hostname);

			// wait till instance is running!
			String state = "";
			int i = 0;
			while (!state.equals("running")) {
				System.out.println("check running " + i);
				describeResults = myJec2.describeInstances(instanceIDs);
				state = describeResults.getResponse().get(0).instances.get(0).state;
				i++;
			}

			// get hostname after running
			describeResults = myJec2.describeInstances(instanceIDs);
			hostname = describeResults.getResponse().get(0).instances.get(0).dnsName;
			System.out.println("hostname after: " + hostname);
			
			//test hostname, if good then proceed, otherwise kill instance and start new one.
			int msPsec = 1000;
			long sleepfor = msPsec * 30; //30 secs
			System.out.println("Sleeping for " + sleepfor);
			Thread.sleep(sleepfor);
			// attempt copy immediately!
		//	psh.execCmd("scp /Users/zacharycancio/Documents/workspace/pjaws/PaW14.paw root@"
//							+ hostname + ":./.");

			// terminate instance
			// myJec2.terminateInstances(instanceIDs);
			// describe instance (verifying terminating)
			try {
				Socket s = new Socket(hostname, 22);
				if(s.isConnected()){
					System.out.println("Connection verified to " + hostname);
				}
				s.close();
			} catch (UnknownHostException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	
			
			

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}